<?php

namespace Common\Model;

use Think\Model;

class UserMoneyModel extends Model {
    public static function newSelf()
    {
        return new self();
    }
    public function findOneById($id)
    {
        return $this->where(['id'=>$id])->find();
    }
    
	public function getUserMoneyInfo($user_id)
	{
	    $info=$this->where(['user_id'=>$user_id])->select();
	    foreach ($info as $k=>$v)
	    {
	        $infos[$v['currency_id']]=$v;
	    }
	    return $infos;
	}
	public function initializationMoneyInfo($user_id,$leavel_id)
	{
	    $currency=CurrencyModel::newSelf()->getInfo();
	    $leavel=LeavelModel::newSelf()->findOneById($leavel_id);
	    foreach ($currency as $k=>$v)
	    {
	        $this->add(['user_id'=>$user_id,'currency_id'=>$v['id'],'num'=>$leavel['start_money'.$v['id']]]);
	    }
	}
	public function checkUpMoneyInfo($user_id,$leavel_id,$old_leavel_id)
	{
	    $currency=CurrencyModel::newSelf()->getInfo();
	    $leavel=LeavelModel::newSelf()->findOneById($leavel_id);
	    $old_leavel=LeavelModel::newSelf()->findOneById($old_leavel_id);
	    foreach ($currency as $k=>$v)
	    {
	        if ($leavel['start_money'.$v['id']]>$old_leavel['start_money'.$v['id']]) {
	            $this->incUserMoney($user_id, $v['id'], $leavel['start_money'.$v['id']]-$old_leavel['start_money'.$v['id']]);
	        }
	    }
	    
	}
	public function findUserMoneyInfoByMoneyId($user_id,$currency_id)
	{
	    return $this->where(['user_id'=>$user_id,'currency_id'=>$currency_id])->find();
	}
	public function decUserMoney($user_id,$currency_id,$num)
	{
	    return $this->where(['user_id'=>$user_id,'currency_id'=>$currency_id])->setDec('num',$num);
	}
	public function incUserMoney($user_id,$currency_id,$num)
	{
	    return $this->where(['user_id'=>$user_id,'currency_id'=>$currency_id])->setInc('num',$num);
	}
}